package com.sservice.web.controller.pc;

import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.service.service.IComService;
import com.sservice.service.service.IInfoService;
import com.sservice.service.service.IUserService;
import com.sservice.service.service.impl.InfoServiceImpl;
import com.sservice.urp.entity.User;
import com.sservice.urp.vo.UserVo;
import com.sservice.web.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.soap.Addressing;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserPcController {
    @Addressing
    private IComService iComService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    IInfoService infoService;

    /**
     * 基于客户系统所在片区进行数据查找
     * @param user
     * @param request
     * @return
     */
    @RequestMapping("queryPianQu")
    @ResponseBody
    public BaseResult queryByPianQu(UserVo user, HttpServletRequest request) {
        if (StringUtils.isNoneBlank(user.getDomain())) {
            String comId = iComService.getComIdByDomain(user.getDomain());
            if (StringUtils.isNoneBlank(comId)) {
                user.setComid(Integer.valueOf(comId));
            }
        }
        String comIds = infoService.getPianQu(user.getComid()+"");
        String[] ids = comIds.split(",");
        List list = iUserService.queryPlainByPage(user);
        for (String id : ids
                ) {
            if (!id.equals(user.getComid())) {
                user.setComid(Integer.valueOf(id));
                List list1 =iUserService.queryPlainByPage(user);
                list.addAll(list1);
            }
        }
        ListResult res = new ListResult(true, list);
        return res;
    }

    @RequestMapping("queryPlain")

    @ResponseBody
    public BaseResult queryPlain(UserVo user, HttpServletRequest request) {
        if (StringUtils.isNoneBlank(user.getDomain())) {
            String comId = iComService.getComIdByDomain(user.getDomain());
            if (StringUtils.isNoneBlank(comId)) {
                user.setComid(Integer.valueOf(comId));
            }
        }
        List<User> list = iUserService.queryPlainByPage(user);
        int total = iUserService.countPlainByPage(user);
        user.getPage();
        Pagination page = new Pagination(user.getPage(), total, user.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
    @RequestMapping("queryLaws")
    @ResponseBody
    public BaseResult queryLaws(UserVo user, HttpServletRequest request) {
//        if (StringUtils.isNoneBlank(user.getDomain())) {
//            String comId = iComService.getComIdByDomain(user.getDomain());
//            if (StringUtils.isNoneBlank(comId)) {
//                user.setComid(Integer.valueOf(comId));
//            }
//        }
        List<User> list = iUserService.queryPlainByPage(user);
        int total = iUserService.countPlainByPage(user);
        user.getPage();
        Pagination page = new Pagination(user.getPage(), total, user.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
